package com.campusnews.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.campusnews.mapper.*;
import com.campusnews.pojo.CommentAgree;
import com.campusnews.pojo.Invitation;
import com.campusnews.pojo.SchoolUser;
import com.campusnews.pojo.SecondComment;
import com.campusnews.service.SecondCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campusnews.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lqc
 * @since 2021-09-26
 */
@Service
public class SecondCommentServiceImpl extends ServiceImpl<SecondCommentMapper, SecondComment> implements SecondCommentService {

    @Autowired
    private InvitationMapper invitationMapper;

    @Autowired
    private SchoolUserMapper schoolUserMapper;

    @Autowired
    private SecondCommentMapper secondCommentMapper;

    @Autowired
    private CommentAgreeMapper commentAgreeMapper;

    /**
     * 传递参数 first_comment（second_id,first_id,invi_id,userId,com_second_id）必须包含
     * @param secondComment
     * @return
     */
    @Override
    public JsonResult deleteSecondComment(SecondComment secondComment) {
        //如果二级评论下面没有评论可以直接删除评论数减少一个，有着修改评论为"该评论已被删除"评论数不变
        //查询是否存在二级评论
//        QueryWrapper<SecondComment> wrapperSecond = new QueryWrapper<>();
//        wrapperSecond.eq("invi_id",secondComment.getInviId())
//                .eq("first_id",secondComment.getFirstId())
////                .eq("user_id",secondComment.getUserId())
//                .eq("com_second_id",secondComment.getSecondId());
//        List<SecondComment> secondCommentList = secondCommentMapper.selectList(wrapperSecond);
//        if (secondCommentList.size()>0){
//            secondComment.setComContent("该评论已被删除");
//            JsonResult jsonResult = updateSecondComment(secondComment);
//            if (jsonResult.getStatus()==200){
//                return JsonResult.build(200,"修改评论为：该评论已被删除且评论数不变",true);
//            }else {
//                return JsonResult.errorMsg("用户删除评论失败");
//            }
//        }else {
//
//        }
        int updateNum = 0;
        int count = 0;
        Invitation invitation = invitationMapper.selectById(secondComment.getInviId());
        Integer num = invitation.getCommentNum();
        if (num>0){
            num-=1;
            invitation.setCommentNum(num);
            updateNum+=invitationMapper.updateById(invitation);
            count++;
        }
        updateNum+=secondCommentMapper.deleteById(secondComment.getSecondId());
        count++;
        if (updateNum==count){
            return JsonResult.build(200,"删除评论数减少一个",true);
        }else {
            return JsonResult.errorMsg("用户删除评论失败");
        }
    }

//    /**
//     * first_comment（second_id,first_id,invi_id,userId,com_second_id）必须包含
//     * @param secondComment
//     * @return
//     */
//    @Override
//    public JsonResult insertSecondComment(SecondComment secondComment) {
//        int updateNum = 0;
//        int count = 0;
//        Invitation invitation = invitationMapper.selectById(secondComment.getInviId());
//        Integer num = invitation.getCommentNum();
//        num+=1;
//        invitation.setCommentNum(num);
//        updateNum+=invitationMapper.updateById(invitation);
//        count++;
//        updateNum+=secondCommentMapper.insert(secondComment);
//        count++;
//        if (updateNum==count){
//            return JsonResult.build(200,"用户插入评论",true);
//        }else {
//            return JsonResult.errorMsg("用户插入评论失败");
//        }
//    }

    /**
     * 修改二级评论的全部信息
     * @param secondComment
     * @return
     */
    @Override
    public JsonResult  updateSecondComment(SecondComment secondComment) {
        int i = secondCommentMapper.updateById(secondComment);
        if (i==1){
            return JsonResult.build(200,"用户修改评论成功",i);
        }else {
            return JsonResult.errorMsg("用户修改评论失败");
        }
    }

    @Override
    public SecondComment getSecondCommentInformation(SecondComment secondComment,int userId) {
        SchoolUser user = schoolUserMapper.selectById(secondComment.getUserId());
        secondComment.setUser(user);
        if (secondComment.getUserId().equals(userId)){
            secondComment.setUserStatue(true);
        }
        QueryWrapper<CommentAgree> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("user_id",userId)
                .eq("invi_id",secondComment.getInviId())
                .eq("second_id",secondComment.getSecondId());
        CommentAgree commentAgree = commentAgreeMapper.selectOne(wrapper1);
        if (commentAgree!=null){
            secondComment.setUserAgreeStatue(true);
        }
        SchoolUser comUser = schoolUserMapper.selectById(secondComment.getComUserId());
        secondComment.setComUser(comUser);
        return secondComment;
    }

    @Override
    public SecondComment getSecondComment(SecondComment secondComment,int userId) {
        secondComment = getSecondCommentInformation(secondComment,userId);
        QueryWrapper<SecondComment> wrapperSecond = new QueryWrapper<>();
        wrapperSecond.eq("invi_id",secondComment.getInviId())
                .eq("first_id",secondComment.getFirstId())
                .eq("com_second_id",secondComment.getSecondId())
                .orderByAsc("create_time");
        List<SecondComment> secondCommentList = secondCommentMapper.selectAllAndDelete(wrapperSecond);
        if (secondCommentList.size()>0){
            for (int i = 0; i < secondCommentList.size(); i++) {
                secondCommentList.set(i,getSecondComment(secondCommentList.get(i),userId));
            }
        }
        secondComment.setSecondComments(secondCommentList);
        return secondComment;
    }
}
